oo::class create DirView {
    superclass TreeView
    variable Lbf Tree Type Cls

    constructor {args} {
        next {*}[list {*}$args {fullpath type}]
        my redefineOnNotebooktabChanged
    }

    method redefineOnNotebooktabChanged {} {
        foreach {vars body} [info class definition ::FilesNotebook onNotebookTabChanged] {
            append body \n
            append body {set f [set [info object namespace $container]::File]}
            append body \n
            append body "[self] openTreeviewAtFile \$f"
            oo::define ::FilesNotebook method onNotebookTabChanged $vars $body
        }
    }

    method openTreeviewAtFile {file} {
        set root {}
        foreach e [file split $file] {
            foreach i [$Tree children $root] {
                if {[$Tree item $i -text] == $e} {
                    set root $i
                    if {[$Tree set $i type] != "file"} {
                        my populateTree $Tree $i
                    }
                    $Tree selection remove [$Tree selection]
                    $Tree selection toggle $i
                    $Tree focus $i
                    $Tree see $i
                    break
                }
            }
        }
    }

    method openSelected {fnb tree node} {
        set f [$tree set $node fullpath]
        if ![file isfile $f] { return }

        set nb [set [info object namespace $fnb]::Notebook]
        $fnb openFile [$fnb nextTabid [$nb index current]] $f
    }

    method populateRoots {tree} {
        foreach dir [lsort -dictionary [file volumes]] {
            my populateTree $tree [$tree insert {} end -text $dir \
              -values [list $dir directory]]
        }
    }

    method populateTree {tree node} {
        if {[$tree set $node type] ne "directory"} {
            return
        }
        set path [$tree set $node fullpath]
        $tree delete [$tree children $node]
        foreach f [lsort -dictionary [glob -nocomplain -dir $path *]] {
            set type [file type $f]
            set id [$tree insert $node end -text [file tail $f] \
              -values [list $f $type]]
            if {[set td [array get Type [string trimleft [file extension $f] .]]] != {}} {
                $tree item $id -image [lindex $td 1]
            }
            if {$type == "directory"} {
                $tree insert $id  0 -text dummy
                $tree item $id -text [file tail $f]
            }
        }
    }
}